//
//  ClassCourseViewModel.m
//  HTP
//
//  Created by admin on 15/11/8.
//  Copyright © 2015年 nit. All rights reserved.
//

#import "ClassCourseViewModel.h"

@implementation ClassCourseViewModel
-(void)getAllCourseClassDate:(NSString *)parentId
{
    tns1_getAllClassTime *getAllClassTime = [[tns1_getAllClassTime alloc] init];
    getAllClassTime.parentId = parentId;
    [WSDLNetWork shareInstance:^interfaceSoapBindingResponse *(interfaceSoapBinding *binding) {
        return [binding getAllClassTimeUsingParameters:getAllClassTime headerElements:nil];
    } withResult:^(NSDictionary *message) {
        [self fetchResult:message];
    } throwError:^(NSError *error) {
        [self fetchError];
    }];
}

-(void)getOneDayCourseClassDetail:(NSString *)parentId date:(NSString *)date{
    tns1_getTodayClassTime *getTodayCourseClass = [[tns1_getTodayClassTime alloc] init];
    getTodayCourseClass.parentId = parentId;
    getTodayCourseClass.date = date;//[[NSDate date] fs_stringWithFormat:@"yyyy-MM-dd"];
    [WSDLNetWork shareInstance:^interfaceSoapBindingResponse *(interfaceSoapBinding *binding) {
        return [binding getTodayClassTimeUsingParameters:getTodayCourseClass headerElements:nil];
    } withResult:^(NSDictionary *message) {
        [self fetchResult:message];
    } throwError:^(NSError *error) {
        [self fetchError];
    }];
}

-(void)getCourseClassDetail:(NSString *)courseGroupClassId{
    tns1_getClassTimeDetailById *getClassTimeDetailById = [[tns1_getClassTimeDetailById alloc] init];
    getClassTimeDetailById.cgcId = courseGroupClassId;
    [WSDLNetWork shareInstance:^interfaceSoapBindingResponse *(interfaceSoapBinding *binding) {
        return [binding getClassTimeDetailByIdUsingParameters:getClassTimeDetailById headerElements:nil];
    } withResult:^(NSDictionary *message) {
        [self fetchResult:message];
    } throwError:^(NSError *error) {
        [self fetchError];
    }];
}

-(void)addCourseClassAccess:(NSString *)accessContent andAccess:(NSString *)access withCourseReordId:(NSString *)crId{
    tns1_addAccessById *addAccess = [[tns1_addAccessById alloc] init];
    addAccess.crAccess = access;
    addAccess.crAccessContent = accessContent;
    addAccess.crId = crId;
    [WSDLNetWork shareInstance:^interfaceSoapBindingResponse *(interfaceSoapBinding *binding) {
        return [binding addAccessByIdUsingParameters:addAccess headerElements:nil];
    } withResult:^(NSDictionary *message) {
        [self fetchResult:message];
    } throwError:^(NSError *error) {
        [self fetchError];
    }];
}

-(void)getAllClassTime:(NSString *)parentId{
    tns1_getTheMonthClassTime * getTheMonth = [[tns1_getTheMonthClassTime alloc] init];
    getTheMonth.parentId = parentId;
    [WSDLNetWork shareInstance:^interfaceSoapBindingResponse *(interfaceSoapBinding *binding) {
        return [binding getTheMonthClassTimeUsingParameters:getTheMonth headerElements:nil];
    } withResult:^(NSDictionary *message) {
        [self fetchResult:message];
    } throwError:^(NSError *error) {
        [self fetchError];
    }];
}

-(void)getCourseClassByDates:(NSString *)startDate andEndDate:(NSString *)endDate withParentId:(NSString *)parentId{
    tns1_getCourseRecordByDates *getCourseByDates = [[tns1_getCourseRecordByDates alloc] init];
    getCourseByDates.startDate = startDate;
    getCourseByDates.endDate = endDate;
    getCourseByDates.parentId = parentId;
    [WSDLNetWork shareInstance:^interfaceSoapBindingResponse *(interfaceSoapBinding *binding) {
        return [binding getCourseRecordByDatesUsingParameters:getCourseByDates headerElements:nil];
    } withResult:^(NSDictionary *message) {
        [self fetchResult:message];
    } throwError:^(NSError *error) {
        [self fetchError];
    }];
}
- (void)getCourseRecordDetail:(NSNumber *)crId{
    tns1_getCourseRecordInfo *getCourseRecordInfo = [[tns1_getCourseRecordInfo alloc] init];
    getCourseRecordInfo.crId = [crId stringValue];
    [WSDLNetWork shareInstance:^interfaceSoapBindingResponse *(interfaceSoapBinding *binding) {
        return [binding getCourseRecordInfoUsingParameters:getCourseRecordInfo headerElements:nil];
    } withResult:^(NSDictionary *message) {
        [self fetchResult:message];
    } throwError:^(NSError *error) {
        [self fetchError];
    }];
}

- (void)getCourseRecordByDates:(NSDate *)startDate endDate:(NSDate *)endDate{
    tns1_getCourseRecordByDates_stdudent *getCourseRecords = [[tns1_getCourseRecordByDates_stdudent alloc] init];
    getCourseRecords.startDate = [startDate fs_stringWithFormat:kDateFormatdate];
    getCourseRecords.endDate = [endDate fs_stringWithFormat:kDateFormatdate];
    getCourseRecords.parentId = [USERCONTENT getUserParentId];
    [WSDLNetWork shareInstance:^interfaceSoapBindingResponse *(interfaceSoapBinding *binding) {
        return [binding getCourseRecordByDates_stdudentUsingParameters:getCourseRecords headerElements:nil];
    } withResult:^(NSDictionary *message) {
        if ([message[INTERFACE_RESULT] isEqualToString:INTERFACE_SUCCESS]) {
            //成功 保存数据到本地数据库
            [MyClassRecord updateMyClassRecord:[MyClassRecord mj_objectArrayWithKeyValuesArray:message[INTERFACE_INFO]]];
        }
        [self fetchResult:message];
    } throwError:^(NSError *error) {
        [self fetchError];
    }];
}
- (void)getCourseRecord:(NSDate *)date{
    tns1_getTodayCourseRecord_student *getCourseRecord = [[tns1_getTodayCourseRecord_student alloc] init];
    getCourseRecord.date = [date fs_stringWithFormat:kDateFormatdate];
    getCourseRecord.parentId = [USERCONTENT getUserParentId];
    [WSDLNetWork shareInstance:^interfaceSoapBindingResponse *(interfaceSoapBinding *binding) {
        return [binding getTodayCourseRecord_studentUsingParameters:getCourseRecord headerElements:nil];
    } withResult:^(NSDictionary *message) {
        if ([message[INTERFACE_RESULT] isEqualToString:INTERFACE_SUCCESS]) {
            //成功 保存数据到本地数据库
            [MyClassRecord updateMyClassRecord:[MyClassRecord mj_objectArrayWithKeyValuesArray:message[INTERFACE_INFO]]];
        }
        [self fetchResult:message];
    } throwError:^(NSError *error) {
        [self fetchError];
    }];
}
+ (NSArray *)getLocalCourseRecourse:(NSDate *)startDate endDate:(NSDate *)endDate{
    NSPredicate *courseRecordInDates = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"cgcDate >= '%@' AND cgcDate <= '%@' AND studentId = %@",[startDate fs_stringWithFormat:kDateFormatdate],[endDate fs_stringWithFormat:kDateFormatdate],[NSNumber numberWithInt:[[USERCONTENT getUserParentId] intValue]]]];
    NSArray *array  = [CourseRecord findByPredicate:courseRecordInDates BySorted:@"cgcDate" ascending:NO];
    return [MyClassRecord toMyClassRecords:array];
}
+ (NSArray *)getLocalCourseRecord:(NSDate *)date{
    NSPredicate *courseRecordInDates = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"cgcDate = '%@'  AND studentId = %@",[date fs_stringWithFormat:kDateFormatdate],[NSNumber numberWithInt:[[USERCONTENT getUserParentId] intValue]]]];
    NSArray *array  = [CourseRecord findByPredicate:courseRecordInDates BySorted:@"cgcDate" ascending:NO];
    return [MyClassRecord toMyClassRecords:array];
}
@end
